from starlette.endpoints import HTTPEndpoint
from starlette.responses import JSONResponse
from model.ModResolventList import ResolventList, resolventListSchema
from database import Session, engine
import datetime


def get_resolvent_info(session, proid: str):
    # 根据问题id查询解决方法
    resolvent_info = session.query(ResolventList).filter(ResolventList.proid == proid).order_by(
        ResolventList.resolid.asc()).all()
    return JSONResponse(resolventListSchema.dump(resolvent_info, many=True).data)


def insert_res(session, res):
    # 新增解决方法
    try:
        session.add(res)
    except Exception as e:
        raise


def get_res_max(session, proid: str):
    # 查询问题最大id值
    res_info = session.query(ResolventList).filter(ResolventList.proid == proid).filter(
        ResolventList.resolid != '99').all()
    list_res = resolventListSchema.dump(res_info, many=True).data
    # max_info = max(list_pro,key=lambda item: item["proid"]) 返回最大值所在元素的整个元素
    max_info = 0
    if len(list_res) > 0:
        max_info = max([item["resolid"] for item in list_res])
    return max_info
